import { logger } from '#src/common/utils/logger.js';

export async function handleServiceError(callback, methodName, logData = {}) {
    const startTime = Date.now();
    try {
        const result = await callback();
        const executionTime = Date.now() - startTime;

        logger.info(`服务执行完成: ${methodName}`, {
            context: methodName,
            executionTime,
            ...logData
        });

        return result;
    } catch (error) {
        const executionTime = Date.now() - startTime;
        const errorContext = {
            ...logData,
            executionTime,
            error: error.message
        };
        logger.error(`服务执行失败: ${methodName}`, errorContext); // 添加错误日志
        throw error;
    }
}